package com.yonyou.pmclouds.plantemplate.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.ConcurrentOperationException;
import com.yonyou.pmclouds.basecom.lock.AutoReleaseLockUtil;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateVO;
import com.yonyou.pmclouds.plantemplate.mapper.ScheduleTemplateMapper;
import com.yonyou.pmclouds.plantemplate.processor.*;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 计划模板服务类
 * @Author: zhangzhonghao
 * @Date: 2020-03-27
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = IScheduleTemplateService.class)
public class ScheduleTemplateServiceImpl implements IScheduleTemplateService {

    private static final String LOCK_PREFIX = "schedule_template";

    @Autowired
    private ScheduleTemplateMapper templateMapper;

    @Autowired
    private ScheduleTemplateInsertBeforeProcessor insertBeforeProcessor;

    @Autowired
    private ScheduleTemplateInsertItemProcessor templateInsertItemProcessor;

    @Autowired
    private AuditInfoUpdateProcessor<ScheduleTemplateVO> auditInfoUpdateProcessor;

    @Autowired
    private ScheduleTemplateUpdateBeforeProcessor updateBeforeProcessor;

    @Autowired
    private ScheduleTemplateUpdateItemProcessor updateItemProcessor;

    @Autowired
    private ScheduleTemplateDeleteAfterProcessor deleteAfterProcessor;

    @Override
    public ScheduleTemplateVO insert(ScheduleTemplateVO templateVO) throws BusinessException {
        if (!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + templateVO.getPkScheduleTemplate())) {
            throw new ConcurrentOperationException();
        }

        if (StringUtils.isNotEmpty(templateVO.getPkScheduleTemplate())) {
            return update(templateVO);
        }

        SingleInsertBpTemplate<ScheduleTemplateVO> insertBpTemplate = new SingleInsertBpTemplate<>(templateMapper, false);

        // 校验必填项、设置默认值
        insertBpTemplate.addBeforeProcessor(insertBeforeProcessor);

        // 保存计划任务
        insertBpTemplate.addBeforeProcessor(templateInsertItemProcessor);

        return insertBpTemplate.insert(templateVO);

    }

    @Override
    public ScheduleTemplateVO update(ScheduleTemplateVO templateVO) throws BusinessException {
        SingleUpdateBpTemplate<ScheduleTemplateVO> updateBpTemplate = new SingleUpdateBpTemplate<>(templateMapper,
                new String[]{ScheduleTemplateVO.NAME, ScheduleTemplateVO.MODIFIER, ScheduleTemplateVO.MODIFY_TIME,
                        ScheduleTemplateVO.CHANGE_FLAG, ScheduleTemplateVO.PK_ORG, ScheduleTemplateVO.TYPE});

        // 添加修改时间、修改人
        updateBpTemplate.addBeforeProcessor(auditInfoUpdateProcessor);

        // 保存前校验
        updateBpTemplate.addBeforeProcessor(updateBeforeProcessor);

        // 修改任务项
        updateBpTemplate.addBeforeProcessor(updateItemProcessor);

        return updateBpTemplate.update(templateVO);
    }

    @Override
    public ScheduleTemplateVO delete(ScheduleTemplateVO templateVO) throws BusinessException {
        SingleDeleteBpTemplate<ScheduleTemplateVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(templateMapper);
        deleteBpTemplate.addAfterProcessor(deleteAfterProcessor);
        return deleteBpTemplate.delete(templateVO);
    }
}
